﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace jsGotoDefinition.VS2010.Helpers
{
    /// <summary>
    /// An object representing an instances of the javascript definition being found
    /// </summary>
    public class FindResult
    {
        public FindResult() {

        }

        public FindResult(string filename, int linenumber, bool isvalid, string searchtext, string linetext) {
            this.LineNumber = linenumber;
            this.FileName = filename;
            this.LineText = linetext;
            this.IsValid = isvalid;
            this.SearchText = searchtext;
        }

        /// <summary>
        /// Constructor that creates the object based on a line take from the Find Results window in VS 2010. Each line has the following format:
        /// D:\development\Juice\Juice.System\Development\Dev1\JuiceWeb\juiceshell.aspx.js(68):    if (ajaxex.FaultCode != null) {
        /// </summary>
        /// <param name="resulttext"></param>
        public FindResult(string resulttext) {
            //split the string at the point of the second : because this follows the line number in ()
            //A return line is as follows: 
            //D:\development\Juice\Juice.System\Development\Dev1\JuiceWeb\juiceshell.aspx.js(68):    if (ajaxex.FaultCode != null) {
            int posi = resulttext.IndexOf(':', 2);
            string withoutcode = resulttext.Substring(0, posi).Trim();
            int openLinePos = withoutcode.LastIndexOf('(');
            if (openLinePos < 0) {
                IsValid = false;
                return;
            }
            IsValid = true;
            int closeLinePos = withoutcode.IndexOf(')', openLinePos);
            this.LineNumber = Int32.Parse(withoutcode.Substring(openLinePos + 1, (closeLinePos - openLinePos - 1)));
            this.FileName = withoutcode.Substring(0, openLinePos);
            this.LineText = resulttext.Substring(posi + 1).Trim();
        }

        /// <summary>
        /// The filename of where the regular expression was found
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// The line number of where the regular expression was found
        /// </summary>
        public int LineNumber { get; set; }

        /// <summary>
        /// Whether or not the entry is a valid find or just information from the Find in results window
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// The text being searched for
        /// </summary>
        public string SearchText { get; set; }

        /// <summary>
        /// The line of text where the SearchText was found
        /// </summary>
        public string LineText { get; set; }
    }
}
